<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>成员管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <meta name="keywords" content="成员管理">
    <link href="//fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;500;700;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css" type="text/css" media="all">
    <link rel="stylesheet" href="css/font-awesome.min.css" type="text/css" media="all">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>

<body>
<section class="w3l-hotair-form">
    <h1>成员管理页面</h1>
    <div class="container">
        <div class="workinghny-form-grid">
            <div class="main-hotair">
                <div class="content-wthree">
                    <h2>删除成员</h2>
                    <form id="deleteMemberForm">
                        <input type="number" class="text" name="memberId" placeholder="成员ID" required>
                        <button class="btn" type="submit">删除</button>
                    </form>

                    <h2>修改成员组别</h2>
                    <form id="updateMemberGroupForm">
                        <input type="number" class="text" name="memberId1" placeholder="成员ID" required>
                        <input type="number" class="text" name="newGroupId" placeholder="新组别ID" required>
                        <button class="btn" type="submit">修改</button>
                    </form>

                    <h2>修改成员名字</h2>
                    <form id="updateMemberNameForm">
                        <input type="number" class="text" name="memberId2" placeholder="成员ID" required>
                        <input type="text" class="text" name="newMemberName" placeholder="新成员名字" required>
                        <button class="btn" type="submit">修改</button>
                    </form>

                    <h2>查询成员</h2>
                    <form id="getMemberForm">
                        <input type="number" class="text" name="memberId3" placeholder="成员ID" required>
                        <button class="btn" type="submit">查询</button>
                    </form>

                    <div id="memberResult"></div>

                    <!-- 新增返回主页按钮 -->
                    <button id="backToHomeBtn" class="btn">返回主页</button>
                </div>
                <div class="w3l_form align-self">
                    <div class="left_grid_info">
                        <img src="images/1.png" alt="管理图片" class="img-fluid">
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    $(document).ready(function() {
        // 删除成员
        $('#deleteMemberForm').on('submit', function(event) {
            event.preventDefault();

            var memberId = $('input[name="memberId"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/deleteMember',
                method: 'POST',
                data: {
                    id: memberId
                },
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 修改成员组别
        $('#updateMemberGroupForm').on('submit', function(event) {
            event.preventDefault();

            var memberId = $('input[name="memberId1"]').val();
            var newGroupId = $('input[name="newGroupId"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/updateMemberGroup',
                method: 'POST',
                data: {
                    id: memberId,
                    groupId: newGroupId
                },
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 修改成员名字
        $('#updateMemberNameForm').on('submit', function(event) {
            event.preventDefault();

            var memberId = $('input[name="memberId2"]').val();
            var newMemberName = $('input[name="newMemberName"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/updateMemberName',
                method: 'POST',
                data: {
                    id: memberId,
                    name: newMemberName
                },
                success: function(response) {
                    alert(response.message);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 查询成员
        $('#getMemberForm').on('submit', function(event) {
            event.preventDefault();

            var memberId = $('input[name="memberId3"]').val();

            $.ajax({
                url: 'http://localhost:8080/Servlet/getMember',
                method: 'GET',
                data: {
                    id: memberId
                },
                dataType: 'json',
                success: function(response) {
                    var result = '<h3>成员信息</h3>';
                    if (response.id) {
                        result += '<p>成员ID: ' + response.id + '</p>';
                        result += '<p>成员名字: ' + response.name + '</p>';
                        result += '<p>小组ID: ' + response.groupId + '</p>';
                    } else {
                        result += '<p>' + response.message + '</p>';
                    }
                    $('#memberResult').html(result);
                },
                error: function(xhr, status, error) {
                    alert('请求失败，请重试');
                }
            });
        });

        // 返回主页
        $('#backToHomeBtn').on('click', function() {
            window.location.href = 'http://localhost:8080/Servlet/Log_HTML/home.html'; // 跳转到主页
        });
    });
</script>
</body>

</html>
